package com.ruoyi.web.group.player.controller;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.group.player.dto.PlayerQueryDTO;
import com.ruoyi.web.group.player.entity.Players;
import com.ruoyi.web.group.player.service.PlayersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 球员信息控制器
 */
@RestController
@RequestMapping("players")
public class PlayersController {
    @Autowired
    private PlayersService playersService;

    /**
     * 删除球员
     * @param id
     * @return
     */
    @GetMapping("del")
    public AjaxResult del(@RequestParam int id){

        if(id != 0 &&  playersService.removeById(id)){
            return AjaxResult.success("操作成功");
        }
        return AjaxResult.error("操作失败");
    }

    /**
     * 添加球员
     * @param players
     * @return
     */
    @PostMapping("add")
    public AjaxResult add(@RequestBody Players players){

        if(players != null ){
            players.setCreatedAt(new Date());
            if(playersService.save(players)){
                return AjaxResult.success("操作成功");
            }
        }
        return AjaxResult.error("操作失败");
    }

    /**
     * 修改球员信息
     * @param players
     * @return
     */
    @PostMapping("upd")
    public AjaxResult upd(@RequestBody Players players){

        if(players != null &&  playersService.updateById(players)){
            return AjaxResult.success("操作成功");
        }
        return AjaxResult.error("操作失败");

    }

    /**
     * 查询球员列表
     * @param playersDTO
     * @return
     */
    @PostMapping ("findList")
    public AjaxResult findList(@RequestBody PlayerQueryDTO playersDTO){
        return AjaxResult.success(playersService. findList(playersDTO));
    }

}
